﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using IM.Common;

namespace IM.Database
{
    public class SqlServerProvider : DataProvider
    {
        /// <summary>
        /// 取所有在线用户。
        /// </summary>
        /// <returns></returns>
        public override DataTable GetAllOnlineUsers()
        {
            using (SqlConnection conn = new SqlConnection(connString))
            {
                try
                {
                    conn.Open();
                    using (SqlCommand cmd = new SqlCommand("select * from [xx] ", conn))
                    {
                        using (SqlDataAdapter adp = new SqlDataAdapter(cmd))
                        {
                            DataTable users = new DataTable();
                            adp.Fill(users);
                            return users;
                        }
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                finally
                {
                    conn.Close();
                }
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="verify_result"></param>
        /// <param name="account_id"></param>
        /// <returns></returns>
        public override bool GetAccountDetail(ref s2c_account_verify_result verify_result, int account_id)
        {
            StringBuilder str = new StringBuilder();
            str.Append("select * from account_detail where account_id = '");
            str.Append(account_id);
            str.Append("'");

            bool bRetcode = false;
            
            using (SqlConnection conn = new SqlConnection(connString))
            {
                try
                {
                    conn.Open();

                    using (SqlCommand cmd = new SqlCommand(str.ToString(), conn))
                    {
                        using (SqlDataReader dr = cmd.ExecuteReader())
                        {
                            while (dr.Read())
                            {
                                verify_result.szPetName = dr.GetString(1);
                                verify_result.nGrade = dr.GetInt32(2);
                                verify_result.szLastLoginIP = dr.GetString(3);
                                verify_result.szMotto = dr.GetString(4);
                                verify_result.nSex = dr.GetInt32(5);
                                verify_result.nYear = dr.GetInt32(6);
                                verify_result.nMonth = dr.GetInt32(7);
                                verify_result.nDay = dr.GetInt32(8);
                                verify_result.nAge = dr.GetInt32(9);
                                verify_result.szXiaoSheng = dr.GetString(10);
                                verify_result.szStar = dr.GetString(11);
                                verify_result.szBlood = dr.GetString(12);
                                verify_result.szCountry = dr.GetString(13);
                                verify_result.szProvince = dr.GetString(14);
                                verify_result.szCity = dr.GetString(15);
                                break;
                            }
                        }
                    }
                    bRetcode =  true;
                }
                catch (Exception ex)
                {
                    bRetcode = false;
                    throw ex;
                }
                finally
                {
                    conn.Close();
                }
                return bRetcode;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="strAccount"></param>
        /// <param name="strPwd"></param>
        /// <param name="account_id"></param>
        /// <returns></returns>
        public override bool VerifyAccount(string strAccount, string strPwd, ref int account_id)
        {
            StringBuilder str = new StringBuilder();
            str.Append("select account_id from account_info where account = '");
            str.Append(strAccount);
            str.Append("' and pwd = '");
            str.Append(strPwd);
            str.Append("'");

            using (SqlConnection conn = new SqlConnection(connString))
            {
                try
                {
                    conn.Open();
                    using (SqlCommand cmd = new SqlCommand(str.ToString(), conn))
                    {
                        try
                        {
                            account_id = Int32.Parse(cmd.ExecuteScalar().ToString());
                        }
                        catch (Exception ex)
                        {
                            throw ex;
                        }
                        finally
                        {
                            conn.Close();
                        }
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                finally
                {
                    conn.Close();
                }
                return account_id > 0;
            }
        }
    }
}
